import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import os
import logging
import copy
import time
from sklearn.cluster import DBSCAN
from sklearn.metrics import silhouette_score
from scipy.spatial.distance import jensenshannon
from collections import defaultdict
from torch.utils.data import Dataset, DataLoader, random_split

class Config:
    def __init__(self):
        # 实验参数
        self.num_clients = 100
        self.num_rounds = 100
        self.local_epochs = 3
        self.batch_size = 32
        self.learning_rate = 0.01
        
        # Non-IID程度 (0.1为高度Non-IID, 0.9为近IID)
        self.non_iid_degree = 0.1
        
        # 动态剪枝参数
        self.pruning_threshold = 0.05
        self.top_k_sparsity = 0.1  # 保留前10%的梯度
        
        # DBSCAN聚类参数
        self.dbscan_eps = 0.3
        self.dbscan_min_samples = 5
        self.cluster_min_size_ratio = 0.2  # 最小簇大小比例
        
        # 知识蒸馏参数
        self.kd_temperature = 3.0
        self.kd_alpha = 0.7
        
        # 隐私保护参数
        self.dp_sigma = 0.5  # 差分隐私噪声强度
        
        # 设备配置
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        
        # 路径配置
        self.log_dir = "results/logs"
        self.model_dir = "results/models"
        self.dataset_path = "datasets/"
        
        # 创建目录
        os.makedirs(self.log_dir, exist_ok=True)
        os.makedirs(self.model_dir, exist_ok=True)
        os.makedirs(self.dataset_path, exist_ok=True)